package org.smart4j.framework.helper;

import org.smart4j.framework.annotation.Inject;
import org.smart4j.framework.util.ArrayUtil;
import org.smart4j.framework.util.CollectionUtil;
import org.smart4j.framework.util.ReflectionUtil;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Cai on 2017/7/28.
 * 依赖注入助手类
 */
public class IocHelper {

    //IOC Inversion of Controll
    static {
        //获取所有的Bean类与Bean实例之间的映射关系（—简称Bean Map—）
        Map<Class<?>, Object> beanMap = new HashMap<Class<?>, Object>();
        if (CollectionUtil.isNotEmpty(beanMap)) {
            //遍历Bean Map
            for (Map.Entry<Class<?>, Object> beanEntry : beanMap.entrySet()) {
                //从BeanMap中获取Bean类与Bean实例
                Class<?> beanClass = beanEntry.getKey();
                Object beanInstance = beanEntry.getValue();
                //获取Bean类定义的所有的成员变量
                Field [] beanfields = beanClass.getDeclaredFields();
                if(ArrayUtil.isNotEmpty(beanfields)){
                    //遍历Bean Field
                    for(Field beanField: beanfields){
                        //判断当前Bean是否带有Inject注解
                        if(beanField.isAnnotationPresent(Inject.class)){
                            //在Bean Map中获取Bean Field的实例
                            Class<?> beanFieldClass = beanField.getType();
                            Object beanFieldInstance = beanMap.get(beanFieldClass);
                            if(beanFieldInstance != null){
                                //通过反射初始化BeanField的值
                                ReflectionUtil.setFiled(beanInstance, beanField, beanFieldInstance);
                            }
                        }
                    }
                }
            }
        }


    }

}
